package preprocessing;

import java.io.*;
import java.util.*;

public class StopWordElimination {
	
	private BufferedReader reader=null;
	private ArrayList<String> stopWords = null;
	private File stopFile = null;
	
	public StopWordElimination()
	{
		stopWords = new ArrayList<String>();
	}
	
	private File getFile()
	{
		stopFile = new File("stop.txt");
		if(stopFile==null)
		  return null;
		else
			return stopFile;
	}
	
    private ArrayList<String> collectedStopWords()
    {
    	try
    	{
    		String readEachLine = null;
    		reader = new BufferedReader(new FileReader(getFile()));
    		while((readEachLine=reader.readLine())!=null)
    		{
    			stopWords.add(readEachLine.toLowerCase());
    		}
    	}
    	catch (IOException e) {
			System.out.println(e.getMessage());
		}
    	catch (Exception e) {
    		System.out.println(e.getMessage());
		}
    	Collections.sort(stopWords);
    	return stopWords;
    }
    /**
     * 
     * @param query
     * @return string with the stop words removed
     */
    
    public ArrayList<String> stopWordsRemoval(ArrayList<String> query)
    {
    	try
    	{
    		for(String q:query)
    		{
    			for(String word:collectedStopWords())
    			{
    				if(word.equals(q.toLowerCase()))
    				{
    					query.remove(q);
    				}
    			}
    		}
    	}
    	catch (Exception e) {
    		System.out.println(e.getMessage());
		}
    	return query;
    }
}
